<?php
namespace Trawind\NetSuite\Repository\Product\Item;

use Trawind\NetSuite\PHPToolkit\RecordRef;
use Trawind\NetSuite\PHPToolkit\RecordRefList;
use Trawind\NetSuite\PHPToolkit\StringCustomFieldRef;
use Trawind\NetSuite\PHPToolkit\CustomFieldList;
use Trawind\NetSuite\PHPToolkit\ListOrRecordRef;
use Trawind\NetSuite\PHPToolkit\SelectCustomFieldRef;
use Trawind\NetSuite\PHPToolkit\LotNumberedInventoryItem;
use Trawind\NetSuite\PHPToolkit\ItemSearchBasic;
use Trawind\NetSuite\PHPToolkit\ItemCostingMethod;
use Trawind\NetSuite\PHPToolkit\itemVendor;
use Trawind\NetSuite\PHPToolkit\ItemVendorList;
use Trawind\NetSuite\Repository\Common;

class Sku extends Common
{
    public $service;

    public $type = 'lotNumberedInventoryItem';

    public $pageSize = 1000;

    public $store = LotNumberedInventoryItem::class;

    public $search = ItemSearchBasic::class;

    public $searchFields = ['internalId', 'displayName'];

    public function __construct($config=null)
    {
        parent::__construct($config);
    }

    protected function setData($object, $data)
    {
        $data = $data['data'];
        foreach ($data['elements']['data'] as $key => $value) {
            $tem = $key + 1;
            $data['customer_elements']['data']['custitem_yaosu_name' . $tem] = $value['name'];
            $data['customer_elements']['data']['custitem_yaosu_value' . $tem] = $value['value'];
        }

        $object->itemId = $data['number'];
        $object->displayName = $data['name'];
        $object->useBins = true; // 使用箱

        // tax schedule
        $object->taxSchedule = new RecordRef();
        $object->taxSchedule->internalId = 1;

        // costing method
        $object->costingMethod = new ItemCostingMethod();
        $object->costingMethod = '_lotNumbered';

        // subsidiary
        $subsidiarys = new RecordRef();
        $subsidiarys->internalId = $data['subsidiary']['data']['external_id'];

        $temSubsidiarys = new RecordRef();
        $temSubsidiarys->internalId = 53;

        $subsidiarys = [$temSubsidiarys, $subsidiarys];
        foreach ($data['shares']['data'] as $k => $v) {
            $itemSubsidiarys = new RecordRef();
            $itemSubsidiarys->internalId = $v['subsidiary'] ? $v['subsidiary']['data']['external_id'] : $v['parent_subsidiary']['data']['external_id'];
            $subsidiarys[] = $itemSubsidiarys;
        }
        $object->subsidiaryList = new RecordRefList();
        $object->subsidiaryList->recordRef = $subsidiarys;

        // vendor
        $vendors = [];
        foreach ($data['vendors']['data'] as $value) {
            $vendor = new RecordRef();
            $vendor->internalId = $value['vendor']['data']['external_id'];

            $itemVendor = new itemVendor();
            $itemVendor->vendor = $vendor;
            $itemVendor->purchasePrice = ($data['review_price'] < $value['purchase_price'] && $data['review_price'] > 0) ? $data['review_price'] : $value['purchase_price'];
            $itemVendor->subsidiary = $value['subsidiary']['data']['external_id'];
            $itemVendor->preferredVendor = $value['is_default'] ? true : false;
            $vendors[] = $itemVendor;
        }
        if ($vendors) {
            $object->itemVendorList = new ItemVendorList();
            $object->itemVendorList->itemVendor = $vendors;
        }
        // custom field
        $customMap = [
            StringCustomFieldRef::class => [
                'custitem24' => 'hs_number', // HS编码
                'custitem_item_spec' => 'name', // 供应商型号规格 (暂时跟商品展示名称一致)
                'custitemm_purpose' => 'use', // 用途
                'custitemm_material' => 'material', // 材质
                'custitem_item_length' => 'packing_length_cm', // 物品长
                'custitem_item_width' => 'packing_width_cm', // 物品宽
                'custitem_item_height' => 'packing_height_cm', // 物品高
                'custitem_invoice_item_name' => 'billing_name', // 开票品名
                'custitem_invoice_unit' => 'billing_unit', // 开票单位
                'custitem19' => 'history_owner.data.full_name', // 产品原始开发人员(要改)
                'custitem20' => 'owner.data.full_name', // 产品所属开发人员
                'custitem21' => 'brand', // 产品品牌
                'custitem_declaredvalue' => 'declare_price', // 申报价值
                'custitem_en_declared_name' => 'declare_english_name', // 申报英文名
                'custitem_cn_declared_name' => 'declare_name', // 申报中文名
                //'custitem_this_sku_cantpurchase' => 'hs_number', // 跟单未确认申报要素不能做采购订单?
                'custitem_cs_goodssku_costprice' => 'purchase_price', // 产品档案价格
                'custitem_cs_preview_costprice' => 'review_price', // 评审价格
                //'costingmethoddisplay' => 'hs_number', // 成本计算方法?
                //'costcategory' => 'hs_number', // 成本类别?
                'purchasedescription' => 'name', // 采购描述等于供应商型号规格
                'custitemgoodssku_memo' => 'memo', // 产品备注
                //'custitem_inspectionstandard' => 'hs_number', // 验货标准?
                //'custitemsku_image' => 'hs_number', // 产品图片?
                'custitem_itemid_in_trawindcloud' => 'id', // 信风易境中货品id
                'custitem_yaosu_name1' => 'customer_elements.data.custitem_yaosu_name1',
                'custitem_yaosu_name2' => 'customer_elements.data.custitem_yaosu_name2',
                'custitem_yaosu_name3' => 'customer_elements.data.custitem_yaosu_name3',
                'custitem_yaosu_name4' => 'customer_elements.data.custitem_yaosu_name4',
                'custitem_yaosu_name5' => 'customer_elements.data.custitem_yaosu_name5',
                'custitem_yaosu_name6' => 'customer_elements.data.custitem_yaosu_name6',
                'custitem_yaosu_name7' => 'customer_elements.data.custitem_yaosu_name7',
                'custitem_yaosu_name8' => 'customer_elements.data.custitem_yaosu_name8',
                'custitem_yaosu_name9' => 'customer_elements.data.custitem_yaosu_name9',
                'custitem_yaosu_value1' => 'customer_elements.data.custitem_yaosu_value1',
                'custitem_yaosu_value2' => 'customer_elements.data.custitem_yaosu_value2',
                'custitem_yaosu_value3' => 'customer_elements.data.custitem_yaosu_value3',
                'custitem_yaosu_value4' => 'customer_elements.data.custitem_yaosu_value4',
                'custitem_yaosu_value5' => 'customer_elements.data.custitem_yaosu_value5',
                'custitem_yaosu_value6' => 'customer_elements.data.custitem_yaosu_value6',
                'custitem_yaosu_value7' => 'customer_elements.data.custitem_yaosu_value7',
                'custitem_yaosu_value8' => 'customer_elements.data.custitem_yaosu_value8',
                'custitem_yaosu_value9' => 'customer_elements.data.custitem_yaosu_value9',
            ],
            SelectCustomFieldRef::class => [
                'custitem_item_type' => 'progress_type_id', // 进程
                'custitem_declaredcurrency' => 'declare_currency_id', // 申报币种
            ]
        ];
        $mapList = [
            'progress_type_id_custitem_item_type' => [
                11 => 1,
                12 => 2,
                13 => 3,
                14 => 4,
            ],
            'declare_currency_id_custitem_declaredcurrency' => [
                1 => 1,
                2 => 2,
            ]
        ];
        $customFields = [];
        foreach ($customMap as $customFieldRef => $customValue) {
            foreach ($customValue as $scriptId => $valueFields) {
                $valueFieldsData = explode('.', $valueFields);
                $value = $data;
                foreach ($valueFieldsData as $valueField) {
                    if (isset($value[$valueField])) {
                        $value = $value[$valueField];
                    } else {
                        break;
                    }
                }
                if (is_array($value)) {
                    continue;
                }
                if ($customFieldRef == SelectCustomFieldRef::class) {
                    $selectValue = new ListOrRecordRef;
                    $selectValue->internalId = $mapList[$valueFields . '_' . $scriptId][$value];
                    $value = $selectValue;
                }
                $customField = new $customFieldRef;
                $customField->value = $value;
                $customField->scriptId = $scriptId;
                $customFields[] = $customField;
            }
        }
        if ($customFields) {
            $object->customFieldList = new CustomFieldList();
            $object->customFieldList->customField = $customFields;
        }
        return $object;
    }
}


